package com.xxq.semester.controller.classmgmt;


import com.xxq.semester.entity.classmgmt.ClassStudents;
import com.xxq.semester.entity.classmgmt.ClassTeachers;
import com.xxq.semester.entity.utils.ResponseEntity;
import com.xxq.semester.service.classmgmt.IClassStudentsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author saki
 * @since 2025-07-13
 */
@RestController
@RequestMapping("/api/class-students")
@Tag(name = "学生端班级管理")
@Transactional
public class ClassStudentsController {
    @Autowired
    private IClassStudentsService classStudentsService;
    @Operation(summary = "加入班级")
    @PostMapping("/addClassStudents")
    public ResponseEntity<String> addClassStudents(@RequestBody ClassStudents classStudents){
        return classStudentsService.addClassStudents(classStudents) ?
                ResponseEntity.success("加入班级成功") :
                ResponseEntity.fail("加入班级失败");
    }
    @Operation(summary = "退出班级")
    @PostMapping("/deleteClassStudents")
    public ResponseEntity<String> deleteClassStudents(@RequestParam Integer classId,@RequestParam Integer studentId){

        return classStudentsService.deleteClassStudents(classId, studentId) ?
                ResponseEntity.success("退出班级成功") :
                ResponseEntity.fail("退出班级失败");
    }
    @Operation(summary = "获取已加入班级列表信息")
    @GetMapping("/getClassStudentsByClassId")
    public ResponseEntity<List<ClassStudents>> getClassStudentsByClassId(Integer studentId){
        return ResponseEntity.success(classStudentsService.getClassStudentsByClassId(studentId));
    }
    @Operation(summary = "通过邀请码加入班级")
    @PostMapping("/getInClassByInviteCode")
    public ResponseEntity<String> getInClassByInviteCode(@RequestParam String inviteCode,@RequestParam Integer studentId){
        Integer classId = ClassTeachers.inviteCodeToInt(inviteCode);
        return classStudentsService.getInClassByInviteCode(classId, studentId) ?
                ResponseEntity.success("加入班级成功") :
                ResponseEntity.fail("加入班级失败");
    }
}
